#include "sqlist.h"

SeqList createSeqList(SIZE_T length)
{
    #ifdef DEBUG
    SIZE_T i;
    #endif
    SeqList pseqList = (SeqList) malloc(sizeof(struct SQList));
    if (pseqList != NULL)
    {
        pseqList->elements = (ELEMTYPE *) malloc(sizeof(ELEMTYPE)*length);
	if (pseqList != NULL)
        {
            pseqList->length = 0;
            pseqList->maxNum = length;
            #ifdef DEBUG
            for (i = 0; i < pseqList->maxNum; i++)
            {
                pseqList->elements[i] = i;
            }
            pseqList->length = pseqList->maxNum;
	    #endif
            return pseqList;
        }
        free(pseqList);
    }
    return NULL;
}

void destroySeqList(SeqList list)
{
    if (list != NULL)
    {
        free(list->elements);
        free(list);
        list = NULL;
    }
}

void printSeqList(SeqList list)
{
    SIZE_T i;
    
    for (i = 0; i < list->length; i++)
    {
        #ifdef DEBUG
        printf("elements[%d] = %d\n", i, list->elements[i]);
        #endif
        printf("%d ", list->elements[i]);
    }
    
    printf("\b\n");
}

SIZE_T lengthSeqList(SeqList list)
{
    if (list != NULL)
    {
        return list->length;
    }
    return 0;
}

int getVertexSeqList(SeqList list, POSITION p, ELEMTYPE *x)
{
    if (list == NULL)
    return -1;
    
    if (p < 0 || p > list->length)
    {
        return 0;
    }
    
    *x = list->elements[p];
    return 1;
}

POSITION locateVertexSeqList(SeqList list, ELEMTYPE x)
{
    POSITION i;

    if (list == NULL)
    {
        return -1;
    }
    
    for (i = 0; i < list->length; i++)
    {
        if (list->elements[i] == x)
        {
            return i;
        }
    }
    
    return -1;
}

int insertPreSeqList(SeqList list, POSITION p, ELEMTYPE x)
{
    POSITION q;

    if (list == NULL)
    {
        return -1;
    }

    if (p < 0 || p > list->length)
    {
        return -1;
    }

    if (list->length == list->maxNum)
    {
        return 0;
    }

    for (q = list->length - 1; q >= p; q--)
    {
        list->elements[q+1] = list->elements[q];
    }
    
    list->elements[p] = x;
    list->length++;
    
    return 1;
}

int insertPostSeqList(SeqList list, POSITION p, ELEMTYPE x)
{
    POSITION q;

    if (list == NULL)
    {
        return -1;
    }

    if (p < 0 || p > list->length)
    {
        return -1;
    }

    if (list->length == list->maxNum)
    {
        return 0;
    }

    if (p == list->length)
    {
        list->elements[p] = x;
        list->length++;
        return 1;
    }

    for (q = list->length - 1; q > p; q--)
    {
        list->elements[q+1] = list->elements[q];
    }
    
    list->elements[p+1] = x;
    list->length++;
    
    return 1;
}

int deleteVertexSeqList(SeqList list, POSITION p)
{
    POSITION q;
    
    if (list == NULL)
    {
        return -1;
    }
    
    if (p < 0 || p > list->length)
    {
        return 0;
    }
    
    for (q = p; q < list->length; q++)
    {
        list->elements[q] = list->elements[q+1];
    }
    list->length--;

    return 1;
}

int deleteElemSeqList(SeqList list, ELEMTYPE x)
{
    POSITION p;
    
    p = locateVertexSeqList(list, x);
    if (p != -1)
    {
        deleteVertexSeqList(list, p);
	return 1;
    }

    return -1;
}

int appendElemSeqList(SeqList list, ELEMTYPE x)
{
    if (list == NULL)
    {
        return -1;
    }
    
    if (list->length+1 == list->maxNum)
    {
        return 0;
    }
    
    list->elements[list->length++] = x;
    return 1;
}
